<template>
	<view class="">

		<dsl-navbar title="添加出行人" bgTopImg="white" isBack=""></dsl-navbar>
		<view class="rowsb padding">
			<view>{{userInfo.userName || '暂无'}}</view>
			<picker mode="date" :value="date " :start="time" end="2090-09-01" @change="DateChange">
				<view class="picker">
					<view>{{date || time}}</view>
				</view>
			</picker>
		</view>

		<view class="bg-white rowsb align-center solid-bottom padding">
			<view>出行人</view>
			<button class="cu-btn bg-red" style="background-color: #29EAC4;" @click="address">选择出行人</button>
		</view>
		<view v-if="addressData">
			<view class="bg-white rowsb align-center solid-bottom padding">
				<view class="text-sm">{{addressData.realName}}</view>
				<view>{{addressData.contactNumber}}</view>
			</view>
		</view>
		<view class="bg-white rowsb margin-top align-center solid-bottom padding" @click="contartMen">
			<view class="text-sm">紧急联系人</view>
			<view class="align-center">
				<view v-if="contart" class="text-sm 	">
					{{contart.name}} : {{contart.mobile}}
				</view>
				<view class="cuIcon-right"></view>
			</view>
		</view>

		<!-- 	<view class="bg-white rowsb margin-top align-center solid-bottom padding">
			<view class="text-sm">过敏原/忌口</view>
			<input type="text" value="" v-model="content" placeholder="请输入" class="text-right" />
		</view> -->
		<view class="bg-white rowsb margin-top align-center solid-bottom padding" @click="tameSelect">
			<view class="text-sm">选择团长</view>
			<view class="align-center">
				<view v-if="team" class="text-sm 	">
					{{team.realName || ''}}
				</view>
				<view class="cuIcon-right"></view>
			</view>
		</view>
		<view class="padding bg-white margin-top">
			<view class="text-sm "> 备注：</view>
			<textarea value="" placeholder="暂未填写" style="width: 100%;" maxlength="999" class="padding-tb"
				v-model="remark" />
		</view>
		<view class="padding text-red">
			<view>报名须知</view>
			<view class="margin-tb-sm">1报名下单，即是接受了我们的服务内容，退团会有违约金。详细费用根据产品的不同数额不同，请务必注意。</view>
			<view>2游客报名请确保身体健康，如有身体疾病，应如实向旅行社进行说明，如有隐瞒，由此带来的健康问题，游客自行负责。</view>

		</view>
		<view class="cu-bar bg-white tabbar border shop submit_bottom" @click="orderSubmit">
			<view style="width: 300rpx;" class="text-center text-red text-bold">￥{{price}}</view>
			<view class="bg-red submit" style="background-color: #29EAC4;">立即支付</view>
		</view>
	</view>
</template>

<script>
	var util = require('../../common/util.js');
	var time = util.formatTime(new Date());
	import {
		post_addOrder,
		getCaptainByUserId
	} from '@/common/path.js'
	export default {
		data() {
			return {
				time: time,
				date: '', //结束时间
				userInfo: uni.getStorageSync('userInfo'),
				addressData: {}, //地址信息
				contart: '', //紧急联系人
				content: '', //忌口的 过敏的
				payType: 1, //支付状态 1微信 0余额
				remark: '', //备注
				tourDataId: '', //商品id
				team: '', //团长信息
				price: '', //总价格


			}
		},
		//生命周期函数
		onReady() {},
		onLoad(option) {
			this.tourDataId = option.id
			this.price = option.price
			uni.removeStorage({
				key: 'team',
			});
		},
		onShow() {
			if (uni.getStorageSync('address')) this.addressData = uni.getStorageSync('address')
			if (uni.getStorageSync('contart')) this.contart = uni.getStorageSync('contart')
			if (uni.getStorageSync('team')) {
				if (uni.getStorageSync('team')) this.team = uni.getStorageSync('team')
			} else {
				if (uni.getStorageSync('idd')) {
					this.getCaptainByUserId()
				}
			}
		},
		methods: {
			//团长信息
			getCaptainByUserId() {
				let data = {
					userId: uni.getStorageSync('idd')
				}
				this.Http.POST(getCaptainByUserId, data)
					.then((res) => {
						console.log('[团长信息]', res);
						if (res.code == 0) {
							this.team = res.data
						}
					})
			},
			//选择出行人
			address() {
				uni.navigateTo({
					url: '/pages/address/index?type=1'
				})
			},
			//选择紧急联系人
			contartMen() {
				uni.navigateTo({
					url: '/pages/user/modules/urgencyList?type=1'
				})
			},
			// 团长选择
			tameSelect() {
				uni.navigateTo({
					url: '/pages/user/team/regimentalSelect?type=1'
				})
			},
			//订单确认选择支付方式
			orderSubmit() {
				let _this = this
				_this.submit()
				return
				uni.showActionSheet({
					itemList: ['微信支付', '余额支付'],
					success: function(res) {
						if (res.tapIndex == 0) {
							_this.payType = 1 //微信1
						} else {
							_this.payType = 0 //余额0
						}
						_this.submit()
					},
					fail: function(res) {
						console.log(res.errMsg);
					}
				});
			},
			//时间选择
			DateChange(e) {
				this.date = e.detail.value
			},
			//提交
			submit() {
				if (this.addressData == '') {
					this.$tools.showToast('请选择出行人资料')
					return
				}
				if (this.contart == '') {
					this.$tools.showToast('请选择紧急联系人')
					return
				}
				if (this.team == '') {
					this.$tools.showToast('请选择团长')
					return
				}

				let data = {
					"buyer": uni.getStorageSync('userInfo').id,
					"payType": this.payType,
					"remark": this.remark, //备注
					"cityCode": uni.getStorageSync('addressId'),
					"orderDetailDto": {
						"tourId": this.tourDataId, //商品id
						"captainId": this.team.id, //团长id
						"note": this.content, //忌口
						"startTime": this.date ? this.date : this.time, //开始时间 ， 默认当前系统时间
						// "endTime": this.date ? this.date : this.time,
						"emergencyContactId": this.contart.id,
						"memberIds": [
							this.addressData.id
						]
					}
				}
				this.Http.POST(post_addOrder, data)
					.then((res) => {
						if (res.code == 0) {
							this.$tools.showToast('下单成功')
							this.requestPayment(res.data)
						} else {
							this.$tools.showToast(res.msg)
						}
					})
			},
			//微信支付
			requestPayment(res) {
				let _this = this
				console.log('[支付参数]', res);
				let datas = {
					nonceStr: res.nonceStr, //随机字符串
					package: res.payPackage, //下单接口返回参数
					timeStamp: res.timeStamp, //时间戳
					signType: res.signType, //签名算法，应与后台一致
					paySign: res.paySign, //签名
				}
				console.log(datas);
				uni.requestPayment({
					nonceStr: res.nonceStr, //随机字符串
					package: res.payPackage, //下单接口返回参数
					timeStamp: res.timeStamp, //时间戳
					signType: res.signType, //签名算法，应与后台一致
					paySign: res.paySign, //签名
					success: function(aaa) {
						uni.showToast({
							title: '支付成功',
							icon: 'none'
						})
					},
					fail: function(err) {
						_this.$tools.showToast(JSON.stringify(err))
						console.log('支付失败===>', err)
					},
				})
			}
		},
		computed: {},
		components: {}
	}
</script>

<style>
	page {
		background-color: #F1F1F1;
	}

	.submit_bottom {
		position: fixed;
		left: 0;
		right: 0;
		bottom: 0;

	}

	.activeText {
		color: #29EAC4;
	}
</style>
